package com.example.tourmemory.data.getdata;

import android.content.Context;

import com.example.tourmemory.R;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatManager {
    private static boolean isThisYear(LocalDateTime dateTime){
        /* 判断时间是否为今年
        输入时间格式: 时间戳
         */
        LocalDateTime now = LocalDateTime.now();
        return dateTime.getYear() == now.getYear();
    }

    private static boolean isToday(LocalDateTime dateTime){
        /* 判断时间是否为今天
        输入时间格式: 时间戳
         */
        LocalDateTime now = LocalDateTime.now();
        return dateTime.getYear() == now.getYear() && dateTime.getMonth() == now.getMonth() && dateTime.getDayOfMonth() == now.getDayOfMonth();
    }

    public static String formatDate(Context context, LocalDateTime dateTime, boolean lineWrap){
        /* 设置日期格式
        输入时间格式: 时间戳
        返回格式: yyyy年\n-MM月-dd日
         */
        if(isToday(dateTime)) {
            return context.getString(R.string.today);
        } else if(isThisYear(dateTime)) {
            return dateTime.getMonth() + context.getString(R.string.month) +
                    dateTime.getDayOfMonth() + context.getString(R.string.day);
        } else {
            String year = dateTime.getYear() + context.getString(R.string.year);
            if(lineWrap) year += '\n';
            return year + dateTime.getMonth() + context.getString(R.string.month) +
                    dateTime.getDayOfMonth() + context.getString(R.string.day);
        }
    }

    public static String formatTime(LocalDateTime dateTime){
        /* 设置时间格式
        输入时间格式: 时间戳
        返回格式: HH:mm
         */
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
        return dateTime.format(timeFormatter);
    }
}
